package com.scsj.supermarket.view.fragment.twokindsbasemodel;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.scsj.supermarket.R;
import com.scsj.supermarket.adapter.IndexGoodsListAdapter;
import com.scsj.supermarket.adapter.QueryShopInTradeAdapter;
import com.scsj.supermarket.base.QueryShopInTradeBase;
import com.scsj.supermarket.bean.IndexGoodsListBean;
import com.scsj.supermarket.bean.QueryShopInTradeBean;
import com.scsj.supermarket.bean.RequestShopInTradeBean;
import com.scsj.supermarket.presenter.QueryShopInTradePresenter;
import com.scsj.supermarket.view.fragment.basemodel.BaseFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.RequestBody;

/**
 * 商品基础列表
 */

public class GoodsListBaseFragment extends BaseFragment implements QueryShopInTradeBase.IView {
    public  String searchText;
    public  boolean startSearch=false;
    private RecyclerView rv;
    private QueryShopInTradeAdapter adapter;
    private QueryShopInTradePresenter queryShopInTradePresenter;

    private String tradeId;
    private double lat;
    private double lon;
    //用来存放商圈下所有店铺的集合
    List<QueryShopInTradeBean.DataBean.ListBean> shopInTrades = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View inflate = inflater.inflate(R.layout.fragment_goods_list_base, null);

        initView(inflate, null);
        initListener();
        initData();

        return inflate;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        queryShopInTradePresenter = new QueryShopInTradePresenter(this);
        rv = view.findViewById(R.id.index_goods_list_rv);
        rv.setLayoutManager(new LinearLayoutManager(getHoldingActivity()));
        rv.setNestedScrollingEnabled(false);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        startGetData();
    }

    public void startGetData() {
//        Log.e("Good::",searchText);
        tradeId = getActivity().getIntent().getStringExtra("tradeId");
        lat = getActivity().getIntent().getDoubleExtra("lat", 0);
        lon = getActivity().getIntent().getDoubleExtra("lon", 0);

        //默认查询商圈下面所有的店铺

        RequestShopInTradeBean requestShopInTradeBean = new RequestShopInTradeBean();
        RequestShopInTradeBean.PageInfoBean pageInfoBean = new RequestShopInTradeBean.PageInfoBean();
        requestShopInTradeBean.setId(tradeId);
        requestShopInTradeBean.setName(searchText);
        requestShopInTradeBean.setLatitude((int) lat);
        requestShopInTradeBean.setLongitude((int) lon);

        //设置每页加载的个数
        //pageInfoBean.setPageNum();
        //设置页数
        //pageInfoBean.setPageSize();
        Gson gson = new Gson();
        String obj = gson.toJson(requestShopInTradeBean);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), obj);
        queryShopInTradePresenter.deliverData(body);
    }


    @Override
    protected int getLayoutId() {
        return 0;
    }

    @Override
    public void showSuccessMsg(String s, QueryShopInTradeBean queryShopInTradeBean) {
        //查询商圈下的店铺
        shopInTrades = queryShopInTradeBean.getData().getList();
        Log.e("店铺查询成功",shopInTrades.size()+"");
        adapter = new QueryShopInTradeAdapter(shopInTrades);
        rv.setAdapter(adapter);

    }

    @Override
    public void showErrorMsg (String s){

    }
}

